interface GroupResult<T = any> {
  key: string
  value: T[]
}

export function groupBy<T = any>(arr: T[], groupFunction: Function) {
  const result: GroupResult<T>[] = []
  arr.forEach((item) => {
    const key = groupFunction(item)
    const group = result.find((resultItem) => resultItem.key === key)
    if (!group) {
      result.push({
        key,
        value: [item]
      })
    } else {
      group.value.push(item)
    }
  })
  return result
}

export function findAndRemove(arr: any[], id: string) {
  const index = arr.findIndex((item) => item.id === id)
  arr.splice(index, 1)
  return arr
}

export function getDataByPage<T = any>(
  data: T[],
  page: number,
  pageSize: number
) {
  return data.slice((page - 1) * pageSize, page * pageSize)
}
